package web

import (
	"ksd-social-api/commons/base/controller"
	"ksd-social-api/modules/special/model/context"
)

type SpecialController struct {
	controller.BaseController
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据专题id查询明细
 */
func (web *SpecialController) GetDetail() {
	// 1: 获取专题id
	specialPath := web.Ctx.Input.Param(":id")
	// 2: 用户id从哪里获取
	userId := web.GetUserId()
	// 浏览数递增
	ipAddr := web.GetIpAddr()
	// 3: 开始查询专题的信息
	vo, err := specialService.GetSpecialByPath(specialPath, userId, web.GetSystemId())
	// 如果查询出错，把错误信息返回。方便定位错误
	if err != nil {
		web.FailCodeMsg(701, "查询失败，原因是：没有此内容")
		return
	}

	if vo.IsDeleted == 1 {
		web.FailCodeMsg(701, "找不到你想要的内容！")
		return
	}

	if vo.Status == 0 {
		web.FailCodeMsg(701, "老师还在准备中，尚未发布！")
		return
	}

	// 保存用户的文章浏览记录
	if web.GetUserId() > 0 {
		specialUserHitsContext := context.SpecialUserHitsContext{}
		specialUserHitsContext.Opid = vo.Id
		specialUserHitsContext.UserId = web.GetUserId()
		specialUserHitsContext.SystemId = web.GetSystemId()
		specialUserHitsContext.SystemName = web.GetSystemName()
		specialUserHitsContext.Username = web.GetUsername()
		specialUserHitsContext.Nickname = web.GetUserNickname()
		specialUserHitsContext.Avatar = web.GetUserAvatar()
		specialUserHitsContext.Uuid = web.GetUuid()
		specialService.SaveUserSpecialHits(&specialUserHitsContext)
	}
	specialService.UpdateViewCount(vo.Id, ipAddr, web.GetSystemId())
	web.Ok(vo)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据专题id查询明细
 */
func (web *SpecialController) GetUserStudyDetail() {
	// 1: 获取专题id
	specialPath := web.Ctx.Input.Param(":id")
	// 2: 用户id从哪里获取
	userId := web.GetUserId()
	// 浏览数递增
	ipAddr := web.GetIpAddr()
	// 3: 开始查询专题的信息
	vo, err := specialService.GetUserStudyDetailByPath(specialPath, userId, web.GetSystemId())
	// 如果查询出错，把错误信息返回。方便定位错误
	if err != nil {
		web.FailCodeMsg(701, "查询失败，原因是：没有此内容")
		return
	}

	if vo.IsDeleted == 1 {
		web.FailCodeMsg(701, "找不到你想要的内容！")
		return
	}

	if vo.Status == 0 {
		web.FailCodeMsg(701, "老师还在准备中，尚未发布！")
		return
	}

	// 保存用户的文章浏览记录
	if web.GetUserId() > 0 {
		specialUserHitsContext := context.SpecialUserHitsContext{}
		specialUserHitsContext.Opid = vo.Id
		specialUserHitsContext.UserId = web.GetUserId()
		specialUserHitsContext.SystemId = web.GetSystemId()
		specialUserHitsContext.SystemName = web.GetSystemName()
		specialUserHitsContext.Username = web.GetUsername()
		specialUserHitsContext.Nickname = web.GetUserNickname()
		specialUserHitsContext.Avatar = web.GetUserAvatar()
		specialUserHitsContext.Uuid = web.GetUuid()
		specialService.SaveUserSpecialHits(&specialUserHitsContext)
	}
	specialService.UpdateViewCount(vo.Id, ipAddr, web.GetSystemId())
	web.Ok(vo)
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  返回专题分页信息（根据栏目查询）
 */
func (web SpecialController) FindSpecialListAndPage() {
	// 1: 定义一个接受分页信息的上下文
	specialSearchContext := context.SpecialSearchContext{}
	// 2: 开始绑定参数
	web.BindJSON(&specialSearchContext)
	specialSearchContext.SystemId = web.GetSystemId()
	// 3：执行查询返回
	web.Ok(specialService.FindSpecialListAndPage(specialSearchContext))
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  返回专题分页信息（根据栏目查询）
 */
func (web SpecialController) ValiatorBuyFlagIdNo() {
	// 1: 获取专题id
	specialId, _ := web.ParseParamUint64("specialId")
	// 2: 用户id从哪里获取
	userId := web.GetUserId()
	// 3：执行查询返回
	web.Ok(specialService.ValiatorBuyFlagIdNo(specialId, userId, web.GetSystemId()))
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  点赞专题
 */
func (web SpecialController) LikeSpecial() {
	specialId, _ := web.ParseParamUint64("specialId")
	// 3：执行查询返回
	flag, err := specialService.LikeSpecial(web.GetUserId(), specialId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  取消点赞
 */
func (web SpecialController) UnLikeSpecial() {
	specialId, _ := web.ParseParamUint64("specialId")
	// 3：执行查询返回
	flag, err := specialService.UnLikeSpecial(web.GetUserId(), specialId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  收藏专题
 */
func (web SpecialController) FavSpecial() {
	specialId, _ := web.ParseParamUint64("specialId")
	// 3：执行查询返回
	flag, err := specialService.FavSpecialByUserId(web.GetUserId(), specialId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 取消专题收藏
 */
func (web SpecialController) UnFavSpecial() {
	specialId, _ := web.ParseParamUint64("specialId")
	// 3：执行查询返回
	flag, err := specialService.UnFavSpecialByUserId(web.GetUserId(), specialId, web.GetSystemId())
	if err != nil {
		web.FailCodeMsg(901, err.Error())
	} else {
		web.Ok(flag)
	}
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc  返回推荐的专题信息
 */
func (web SpecialController) FindPushSpecialListAndPage() {
	// 1: 定义一个接受分页信息的上下文
	specialSearchContext := context.SpecialSearchContext{}
	// 2: 开始绑定参数
	web.BindJSON(&specialSearchContext)
	specialSearchContext.SystemId = web.GetSystemId()
	// 3：执行查询返回
	web.Ok(specialService.FindPushSpecialListAndPage(specialSearchContext))
}
